package org.yn.base.common;

import io.swagger.annotations.Api;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.yn.base.service.IFastDfsService;
import org.yn.base.util.AjaxResult;
import org.yn.exception.ResponseCode;

@Api("文件上传接口")
@RestController
@RequestMapping("/common")
public class CommonController {

    @Autowired
    private IFastDfsService fastDfsService;
    @PostMapping("/fastDfs")
    public AjaxResult upload(MultipartFile file){

        try {
            String path = fastDfsService.upload(file); //上传成功返回地址
            return AjaxResult.success(path); //地址返回前端
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage(), ResponseCode.RESPONSE_CODE_500.getCode());
        }

    }


    @DeleteMapping("/fastDfs")
    public AjaxResult delete(String path){

        try {
            fastDfsService.delete(path);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage(), ResponseCode.RESPONSE_CODE_500.getCode());
        }

    }

}
